﻿@model EntityViewModel
@{
    ViewBag.Title = Model.Metadata.Name + " 列表";
    ViewBag.FieldWidth = 100 / Model.Headers.Length * 400 * 0.01 + "px";
    object area = ViewContext.RouteData.DataTokens["Area"];
    if (area == null)
    {
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    else
    {
        Layout = "~/Areas/" + area.ToString() + "/Views/Shared/_Layout.cshtml";
    }
}
<style>
    .op {
        width:200px;
        list-style: none;
        padding: 0;
    }
    .op li{
        float:left;
        text-align:center;
    }
    .autocut {  
        width:@ViewBag.FieldWidth;  
        overflow:hidden;  
        white-space:nowrap;  
        text-overflow:ellipsis;  
        -o-text-overflow:ellipsis;  
        -icab-text-overflow: ellipsis;  
        -khtml-text-overflow: ellipsis;  
        -moz-text-overflow: ellipsis;  
        -webkit-text-overflow: ellipsis;  
    }  
    .op a{
        padding: 3px 2px;
    }
</style>
<ol class="breadcrumb">
    <li><a href="@Url.Action("Index","Home")">主页</a></li>
    <li class="active">@Model.Metadata.Name</li>
</ol>
@if (Model.SearchItem.Length > 0)
{
    <div class="alert alert-success alert-dismissable">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <p><h4>搜索条件</h4></p>
        @for (int i = 0; i < Model.SearchItem.Length; i++)
        {
            Html.RenderPartial("SearchItem", Model.SearchItem[i]);
        }
    </div>
}
<div class="row">
    @if (Model.Parent != null)
    {
        <link href="~/Content/jstree.css" rel="stylesheet" />
        <script src="~/Scripts/jstree.js"></script>
        <div class="col-md-3">
            <div class="panel panel-default">
                <div class="panel-heading">菜单</div>
                <div class="panel-body" id="parentview">
                    <ul>
                        <li class="jstree-open">
                            <a data-nav="true" href="@Html.Raw(Url.Action("Index"))">All</a>
                            <ul>
                                @{Html.RenderPartial("ParentView", Model.Parent);}
                            </ul>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <script type="text/javascript">
            $('#parentview').bind('select_node.jstree', function (node, selected) {
                window.location.href = selected.node.a_attr.href;
            });
            $('#parentview').jstree();
        </script>
    }
    <div class="@(Model.Parent == null ? "col-md-12":"col-md-9")">
        @if (Model.Metadata.AddRoles.All(t => User.IsInRole(t)))
        {
            <a href="@Html.Raw(Url.Action("Create"))" class="btn btn-primary btn-lg" style=" margin-bottom: 26px;">创建</a>
        }
        @if (Model.Metadata.SearchProperties.Length > 0)
        {
                <button class="btn btn-primary btn-lg" data-toggle="modal"
                        data-target="#myModal" style="margin-bottom: 20px;">
                    搜索
                </button>
                <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
                     aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close"
                                        data-dismiss="modal" aria-hidden="true">
                                    &times;
                                </button>
                                <h4 class="modal-title" id="myModalLabel">
                                    搜索条件
                                </h4>
                            </div>
                            <div class="modal-body">
                                <form id="form" class="form-horizontal" role="form" action="@Url.Action("Index")" method="get">
                                    @for (int i = 0; i < @Model.Metadata.SearchProperties.Length; i++)
                                    {
                                        <div class="form-group has-feedback">
                                            <label class="col-sm-2 control-label">@Model.Metadata.SearchProperties[i].Name</label>
                                            <div class="col-sm-8">
                                                @{switch (Model.Metadata.SearchProperties[i].Type)
                                                {
                                                    case System.ComponentModel.DataAnnotations.CustomDataType.Date:
                                                    case System.ComponentModel.DataAnnotations.CustomDataType.DateTime:
                                                        <div class="input-group col-md-7">
                                                            <input type="date" class="form-control col-md-3" id="Search.@( Model.Metadata.SearchProperties[i].Property.Name).Start" name="Search.@( Model.Metadata.SearchProperties[i].Property.Name).Start" />
                                                            <span class="input-group-addon">To</span>
                                                            <input type="date" class="form-control col-md-3" id="Search.@( Model.Metadata.SearchProperties[i].Property.Name).End" name="Search.@( Model.Metadata.SearchProperties[i].Property.Name).End" />
                                                        </div>
                                                        <script type="text/javascript">
                                                            $('#Search.@(Model.Metadata.SearchProperties[i].Property.Name).Start').datepicker();
                                                            $('#Search.@(Model.Metadata.SearchProperties[i].Property.Name).End').datepicker();
                                                        </script>
                                                        break;
                                                    case System.ComponentModel.DataAnnotations.CustomDataType.Currency:
                                                    case System.ComponentModel.DataAnnotations.CustomDataType.Integer:
                                                    case System.ComponentModel.DataAnnotations.CustomDataType.Number:
                                                    <div class="input-group col-md-7">
                                                        <input type="number" class="form-control col-md-3" id="Search.@(Model.Metadata.SearchProperties[i].Property.Name).Start" name="Search.@( Model.Metadata.SearchProperties[i].Property.Name).Start" />
                                                        <span class="input-group-addon">To</span>
                                                        <input type="number" class="form-control col-md-3" id="Search.@(Model.Metadata.SearchProperties[i].Property.Name).End" name="Search.@( Model.Metadata.SearchProperties[i].Property.Name).End" />
                                                    </div>
                                                    <script type="text/javascript">
                                                        $('#Search.@( Model.Metadata.SearchProperties[i].Property.Name).Start').change(function () {
                                                            var number = new Number($(this).val());
                                                            if (isNaN(number)) {
                                                                $(this).val(0);
                                                                return;
                                                            }
                                                        });
                                                        $('#Search.@( Model.Metadata.SearchProperties[i].Property.Name).End').change(function () {
                                                            var number = new Number($(this).val());
                                                            if (isNaN(number)) {
                                                                $(this).val(0);
                                                                return;
                                                            }
                                                        });
                                                    </script>
                                                        break;
                                                    case System.ComponentModel.DataAnnotations.CustomDataType.Other:
                                                        if (Model.Metadata.SearchProperties[i].CustomType == "Enum")
                                                        {
                                                            EntityExtension.EnumItem[] items;
                                                            if (Model.Metadata.SearchProperties[i].Property.PropertyType.IsGenericType)
                                                            {
                                                                items = Html.EnumAnalyze(Model.Metadata.SearchProperties[i].Property.PropertyType.GetGenericArguments()[0]);
                                                            }
                                                            else
                                                            {
                                                                items = Html.EnumAnalyze(Model.Metadata.SearchProperties[i].Property.PropertyType);
                                                            }
                                                            <select name="Search.@Model.Metadata.SearchProperties[i].Property.Name" class="form-control">
                                                                @if (Model.Metadata.SearchProperties[i].Property.PropertyType.IsGenericType)
                                                                {
                                                                    <option value="" selected="selected"></option>
                                                                }
                                                                @for (int s = 0; s < items.Length; s++)
                                                                {
                                                                    <option value="@items[s].Value">@items[s].Name</option>
                                                                }
                                                            </select>
                                                        }
                                                        else
                                                        {
                                                            <input type="text" class="form-control" name="Search.@Model.Metadata.SearchProperties[i].Property.Name" />
                                                        }
                                                        break;
                                                    case System.ComponentModel.DataAnnotations.CustomDataType.Boolean:
                                                    <select class="form-control" name="Search.@Model.Metadata.SearchProperties[i].Property.Name">
                                                        <option value="" selected="selected"></option>
                                                        <option value="True">Yes</option>
                                                        <option value="False">No</option>
                                                    </select>
                                                        break;
                                                    case System.ComponentModel.DataAnnotations.CustomDataType.Sex:
                                                    <select class="form-control" name="Search.@Model.Metadata.SearchProperties[i].Property.Name">
                                                        <option value="" selected="selected"></option>
                                                        <option value="True">男</option>
                                                        <option value="False">女</option>
                                                    </select>
                                                        break;
                                                    default:
                                                    <input type="text" class="form-control" name="Search.@Model.Metadata.SearchProperties[i].Property.Name" />
                                                        break;
                                                }
                                                }
                                            </div>
                                        </div>
                                    }
                                    <div class="form-group">
                                        <div class="col-sm-offset-2 col-sm-10">
                                            <input type="submit" class="btn btn-primary" value="搜索" />
                                            <input type="hidden" name="search" value="True" />
                                        </div>
                                    </div>
                                </form>
                            </div>

                        </div><!-- /.modal-content -->
                    </div><!-- /.modal -->
                </div>
      
        }
        <div class="panel panel-default">

            <!-- Default panel contents -->
            <div class="panel-heading">
                列表
            </div>
 
            <!-- Table -->
            <div class="panel-body">
                <table class="table table-bordered">
                    <thead>
                        
                            @for (int i = 0; i < Model.Headers.Length; i++)
                            {
                                <th style="width:@ViewBag.FieldWidth; line-height: 3.2; text-align:center;" title="@Model.Headers[i].Description">@(Model.Headers[i].ShortName ?? Model.Headers[i].Name)</th>
                            }
                            <th style="line-height: 3.2; text-align:center;">操作</th>
                       
                    </thead>
                    <tbody>
                        @for (int i = 0; i < Model.Items.Length; i++)
                        {
                            <tr>
                                @for (int b = 0; b < Model.Headers.Length; b++)
                                {
                                    <td style="width:@ViewBag.FieldWidth">
                                        <div class="autocut">
                                            @EntityValueConverter.GetConverter(Model.Headers[b]).ConvertToString(Model.Headers[b].Property.GetValue(Model.Items[i]))
                                        </div>
                                    </td>
                                }
                                <td class="text-center" style="width: 64px; text-align:center;">
                                    <ul class="op">
                                        <li><a class="btn" href="@Html.Raw(Url.Action("Detail", new { id = Model.Items[i].Index }))"><i class="glyphicon glyphicon-list-alt"></i>查看</a></li>
                                        @if (Model.Metadata.EditRoles.All(t => User.IsInRole(t)))
                                        {
                                            <li><a class="btn" href="@Html.Raw(Url.Action("Edit", new { id = Model.Items[i].Index }))"><i class="glyphicon glyphicon-edit"></i>编辑</a></li>
                                        }
                                        @if (Model.Metadata.RemoveRoles.All(t => User.IsInRole(t)))
                                        {
                                            <li><a class="btn" onclick="return confirm('确定删除嘛？')" href="javascript:remove('@Model.Items[i].Index')"><i class="glyphicon glyphicon-remove"></i>删除</a></li>
                                        }
                                        @for (int c = 0; c < Model.Buttons.Length; c++)
                                        {
                                            MvcViewButton button = Model.Buttons[c] as MvcViewButton;
                                            <li><a class="btn" href="@Html.Raw(Url.Action(@button.StaticLink, new { id = Model.Items[i].Index }))"><i class="glyphicon glyphicon-list-alt"></i>@button.Name</a></li>                                        
										}
                                  </ul>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
                <div class="row">
                    <div class="col-md-4">
                        @{Html.PaginationButton(Model);}
                    </div>

                    <div class="col-md-4 text-right">
                        @{Html.Pagination(Model);}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Result</h4>
            </div>
            <div class="modal-body">
                <p>This item is reference by other item, can not delete.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    function remove(id) {
        $.ajax({
            url: '@Html.Raw(Url.Action("Remove"))/' + id,
            cache: false,
            type: 'POST',
            success: function () {
                location.reload();
            },
            error: function () {
                $('#myModal').modal('show');
            }
        })
    }
</script>